﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using Screen = UnityEngine.Screen;

namespace Assets.Scripts.Tool
{
    /// <summary>
    /// 分辨率适配工具
    /// </summary>
    public class FitResulotionTool : MonoBehaviour
    {
        public float constantSize = 540;//1920*1080屏幕下的相机的大小、
        private float aspectRatio;//当前屏幕的比例
        private float constantRatio = 9.0f / 16;//屏幕的固定比例
        private float screenWidth;
        private float screenHeight;

        void Start()
        {
            screenHeight = Screen.height;
            screenWidth = Screen.width;
            aspectRatio = (float)Screen.height / Screen.width;
            ChangeCameraOrthographicSize();
        }

        void Update()
        {
            if (Math.Abs(screenHeight - Screen.height) > 1 || Math.Abs(screenWidth - Screen.width) > 1)//浮点数可能一直都不会相等
            {
                screenHeight = Screen.height;
                screenWidth = Screen.width;
                aspectRatio = (float)Screen.height / Screen.width;
                ChangeCameraOrthographicSize();
            }
        }

        private void ChangeCameraOrthographicSize()
        {
            if (aspectRatio < constantRatio)
            {
                GetComponent<Camera>().orthographicSize = constantSize;
            }
            else
            {
                GetComponent<Camera>().orthographicSize = aspectRatio / constantRatio * constantSize;
            }
        }
    }
}